package com.sms.web.system.dao;

import java.util.Date;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import com.sms.web.system.entity.SysUser;

/**
 * @desc 
 * @author WuJiangWu
 * @date 2018年7月24日上午12:38:03
 */
public interface SysUserDao extends JpaRepository<SysUser, String>, JpaSpecificationExecutor<SysUser> {
    
    /**
     * 登陆
     * @param usernmae
     * @param password
     * @return
     */
    @Query("FROM SysUser WHERE delFlag='0' AND username = ?1 AND password = ?2 ")
    public SysUser login(String usernmae, String password);
    
	/**
	 * 根据id逻辑删除用户
	 * @param id
	 * @return
	 */
	@Modifying
    @Query("update SysUser set delFlag='1' where id = ?1")
    public int setDelFlag(String id);
    
    /**
     * 通过sql语句修改用户最后登录时间，因为该字段被设置为了updateable=false
     * @param lastLoginDate 新lastLoginDate
     * @param id id
     * @return xx
     */
    @Modifying
    @Query("update SysUser set lastLoginDate=?1 where id = ?2")
    public int updateLastLoginDate(Date lastLoginDate, String id);
    
    /**
     * 根据登录输入的账户查询用户信息
     * @param account xx
     * @param tenantId 租户id
     * @return xx
     */
    @Query("FROM SysUser WHERE delFlag='0' AND usernmae = ?1")
    public SysUser findByUsername(String usernmae);
    
    /**
     * 通过sql语句修改密码，因为密码字段被设置为了updateable=false
     * @param newPassword 新password
     * @param date date
     * @param userId id
     * @return xx
     */
    @Modifying
    @Query("update SysUser set password=?1,updateDate=?2 where id = ?3")
    public int updatePasswordById(String newPassword, Date date, String userId);
}
